package com.gilgamesh.mall.product.service.impl;

import com.gilgamesh.mall.product.dao.ImageDao;
import com.gilgamesh.mall.product.service.ImageService;
import com.gilgamesh.mall.sdk.bean.product.ImageEntity;
import com.gilgamesh.mall.sdk.dto.ResponseDTO;
import com.gilgamesh.mall.sdk.dto.product.ImageDTO;
import com.gilgamesh.mall.sdk.util.BeanUtil;
import com.gilgamesh.mall.sdk.util.CollectionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Peiyao.Wang on 2021/1/14
 *
 * @author Peiyao.Wang
 * @date 2021/1/13
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ImageServiceImpl implements ImageService {

    final ImageDao imageDao;

    @Override
    public ResponseDTO<ImageDTO> getImageById(Long id) {
        ResponseDTO<ImageDTO> responseDTO = new ResponseDTO<>();
        ImageDTO imageDTO = imageDao.getById(id);
        responseDTO.setData(imageDTO);
        if(BeanUtil.isLogicalNull(responseDTO)){
            responseDTO.setCode("0");
            responseDTO.setMessage("success");
            responseDTO.setSuccess(Boolean.TRUE);
        }
        return responseDTO;
    }

    @Override
    public ResponseDTO<List<ImageDTO>> listImageDTOl(ImageEntity imageEntity) {
        if (log.isDebugEnabled()) {
            log.debug("imageEntity is,{}", imageEntity);
        }
        ResponseDTO<List<ImageDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(imageDao.listImages(imageEntity));
        if (CollectionUtil.isNotEmpty(responseDTO.getData())) {
            responseDTO.setCode("0");
            responseDTO.setMessage("success");
            responseDTO.setSuccess(Boolean.TRUE);
        }
        return responseDTO;
    }
}
